package com.burgeon.backend.SHOP.controller;
/**
 * 砍价
 */

import com.burgeon.backend.SHOP.entity.BargainBean;
import com.burgeon.backend.SHOP.entity.BargainOrder;
import com.burgeon.backend.SHOP.entity.BargainVO;
import com.burgeon.backend.SHOP.entity.PayData;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.BargainService;
import com.burgeon.backend.SHOP.service.ex.OrderExceptionApp;
import com.burgeon.backend.SHOPBACKEND.rest.GetGoodsCode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("shop/bargain")
public class BargainController extends BaseController {
    @Autowired
    private BargainService bargainService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @RequestMapping("getMailFee")
    public ResponseResult<HashMap> getMailFee(String barid) {

        HashMap data = bargainService.getMailFee(barid);

        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    //砍价领取页面数据
    @RequestMapping("bargainOrderData")
    public ResponseResult<BargainOrder> bargainOrderData(String barid) {

        BargainOrder data = bargainService.bargainOrderData(barid);

        return new ResponseResult<BargainOrder>(SUCCESS, data);
    }

    @RequestMapping("store")
    public ResponseResult<HashMap> getStore(String longitude, String latitude, HttpSession session) {
        String token = session.getAttribute("token").toString();

        HashMap data = bargainService.showStore(longitude, latitude, token);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    /**
     * 砍价完成提交订单
     *
     * @param barid
     * @param session
     * @return
     */
    @RequestMapping("submitBar")
    public ResponseResult<PayData> submitBar(String barid, String type, String aid, String store,String distance,
                                             String phoneZT, String nameZT, String date,@RequestParam(value="comment",required=false, defaultValue=" ") String comment,
                                             HttpSession session) {
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        PayData data = bargainService.submitBargain(barid, type, aid, store,distance, phone, date, uid, openid, phoneZT, nameZT, token, comment);
        return new ResponseResult<PayData>(SUCCESS, data);
    }


    @RequestMapping("hew")
    public ResponseResult<HashMap> hewBargainitem(String brid, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        HashMap data = bargainService.hewBargain(brid, uid, token);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @RequestMapping("item")
    public ResponseResult<BargainVO> showBargainitem(String brid, HttpSession session) {
        String token = session.getAttribute("token").toString();
        BargainVO data = bargainService.getBargainItem(brid, token);
        return new ResponseResult<BargainVO>(SUCCESS, data);
    }

    @RequestMapping("create")
    public ResponseResult<HashMap> createBargain(String barid, String aid, String code,
                                                 String color, String size, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        System.err.println("barid=" + barid + "aid=" + aid + "code=" + code + "color=" + color + "size=" + size);
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code,color,size);

        if (null == barcode){
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        }else {
            code = barcode;
        }
        HashMap data = bargainService.createBargain(barid, uid, phone, openid, code, color + "-" + size, token);

        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @RequestMapping("list")
    public ResponseResult<List<BargainBean>> showBargainList(HttpSession session) {
        String uid = "null";//未登录用户
        String token = session.getAttribute("token").toString();
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }
        List<BargainBean> data = bargainService.showBargainList(uid, token);
        return new ResponseResult<List<BargainBean>>(SUCCESS, data);
    }

}
